package com.ga.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;

import com.ga.model.User;

public class GaxFilter implements Filter{
	@Override
	public void doFilter(ServletRequest req, ServletResponse rep, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request=(HttpServletRequest) req;
		HttpServletResponse response=(HttpServletResponse) rep;
		
		boolean b=false;
		String passfile[]={".css",".js",".jpg",".png",".mp4",".json"};
		String passhtml[]={"/bm/login.html","403.html","404.html","405.html","500.html","503.html"};
		String passurl[]={"user/login"};
		String sp=request.getServletPath();
		if(sp.equals("/")) b=true;
		for (String p : passfile) {
			if(sp.lastIndexOf(p)>=0) b=true;
		}
		for (String p : passhtml) {
			if(sp.lastIndexOf(p)>=0) b=true;
		}
		for (String p : passurl) {
			if(sp.lastIndexOf(p)>=0) b=true;
		}
		User user=(User) request.getSession().getAttribute("user");
		if(user!=null) b=true;
		else if("dev".equals(PropertiUtil.application.get("mode"))) {
			User u=new User();
			u.set("uname", "develop@ga.com");
			u.set("pwd", "develop123");
			u.set("nickname", "develop");
			u.set("avatar", "resources/avatar/dev.jpg");
			request.getSession().setAttribute("user", u);
			b=true;
		}
		if(b) chain.doFilter(req, rep);
		else{
			response.sendRedirect(request.getContextPath()+"/pages/bm/login.html");
		}
		
	}
	@Override
	public void destroy() {
		
	}
	@Override
	public void init(FilterConfig arg0) throws ServletException {
		
	}
}
